#include <poker.h>

struct tractor_t *tractor_init(void)
{
    struct tractor_t *tractor = NULL;
    int ret;

    tractor = (struct tractor_t *)malloc(sizeof(struct tractor_t));
    ERRP(NULL == tractor, goto ERR1, 1, "malloc tractor!\n");

    memset(tractor, 0, sizeof(struct tractor_t));

    tractor->sdl = sdl_init(WIDTH, HEIGHT, BPP, KYO_VIDEO | KYO_NET);
    ERRP(NULL == tractor->sdl, goto ERR2, 1, "sdl_init!\n");

    ret = load_sdl_res(tractor);
    ERRP(-1 == ret, goto ERR3, 1, "load_sdl_res!\n");

    tractor->btn_sell = button_create(tractor->sdl, tractor->pic_btn_sell, sell_handle, tractor);
    tractor->btn_bottom = button_create(tractor->sdl, tractor->pic_btn_bottom, bottom_handle, tractor);
    tractor->btn_cancel = button_create(tractor->sdl, tractor->pic_btn_cancel, cancel_handle, tractor);
    tractor->btn_lord = button_create(tractor->sdl, tractor->pic_btn_lord, lord_handle, tractor);

    tractor->who = -1;
    tractor->lord = -1;

    srand(time(NULL));

    shuffle(tractor->poker);

    //net_init(tractor);

    deal_card(tractor);

    register_mouse(tractor->sdl, SDL_BUTTON_LEFT, mouse_handle, tractor);

    show_tractor(tractor);

    return tractor;
ERR3:
    sdl_exit(tractor->sdl);
ERR2:
    free(tractor);
ERR1:
    return NULL;
}

void tractor_exit(struct tractor_t *tractor)
{
    button_destroy(tractor->btn_sell);
    button_destroy(tractor->btn_bottom);
    button_destroy(tractor->btn_cancel);
    button_destroy(tractor->btn_lord);
    destroy_sdl_res(tractor);
    sdl_exit(tractor->sdl);
    free(tractor);
}

int main(void)
{
    struct tractor_t *tractor = NULL;

    tractor = tractor_init();

    run_key(tractor->sdl);

    tractor_exit(tractor);

    return 0;
}
